﻿using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.XtraSplashScreen;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XCI.Extensions;
using XCI.Helper;

namespace XCI.Windows.Core
{
    public class XCIOverlayForm
    {
        private OverlayTextPainter _overlayText = null;
        private IOverlaySplashScreenHandle _handle = null;

        public XCIOverlayForm()
        {
            _overlayText = new XCIOverlayTextPainter();
        }

        public XCIOverlayForm(OverlayTextPainter overlayText)
        {
            _overlayText = overlayText;
        }

        /// <summary>
        /// 显示加载动画窗体
        /// </summary>
        /// <param name="owner">所属窗体</param>
        /// <param name="text">文本消息</param>
        /// <param name="options">选项</param>
        /// <returns>返回动画句柄</returns>
        public void Show(Control owner, string text = null, OverlayWindowOptions options = null)
        {
            if (options == null)
            {
                options = OverlayWindowOptions.Default;
            }
            options.CustomPainter = new OverlayWindowCompositePainter(_overlayText);
            if (_handle == null)
            {
                if (owner.Created)
                {
                    _handle = SplashScreenManager.ShowOverlayForm(owner, options);
                }
                else
                {
                    _handle = SplashScreenManager.ShowOverlayForm(owner.FindForm(), options);
                }
            }
            if (text.IsNotEmpty())
            {
                SetText(text);
            }
        }

        /// <summary>
        /// 设置文本
        /// </summary>
        /// <param name="text">文本消息</param>
        public void SetText(string text)
        {
            _overlayText.Text = text;
        }

        /// <summary>
        /// 设置颜色
        /// </summary>
        /// <param name="color">文本颜色</param>
        public void SetColor(Color color)
        {
            _overlayText.Color = color;
        }

        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="font">文本字体</param>
        public void SetFont(Font font)
        {
            _overlayText.Font = font;
        }

        /// <summary>
        /// 关闭加载动画窗体
        /// </summary>
        public void Close()
        {
            if (_handle != null)
            {
                SplashScreenManager.CloseOverlayForm(_handle);
            }
        }
    }

    public class XCIOverlayTextPainter : OverlayTextPainter
    {
        public XCIOverlayTextPainter()
        {
            this.Font = DevExpress.Utils.AppearanceObject.DefaultFont;
        }

        protected override void DrawText(GraphicsCache cache)
        {
            var f = AppearanceObject.ControlAppearance.GetStringFormat();
            f.Alignment = StringAlignment.Center;
            cache.DrawString(this.Text, this.GetFont(), cache.GetSolidBrush(this.Color), this.Bounds, f);
        }

        protected override Rectangle CalcTextBounds(OverlayLayeredWindowObjectInfoArgs drawArgs)
        {
            var rect = drawArgs.Bounds;
            return new Rectangle(rect.X, rect.Y + drawArgs.ImageBounds.Height, rect.Width, rect.Height);
        }
    }
}
